home *** CD-ROM | disk | FTP | other *** search
- Path: EU.net!sun4nl!ittpub!ittpub!nntp
- Newsgroups: comp.lang.c++
- Subject: Re: Non-standard exception handling
- Message-ID: <1996Jan23.173823.1753@ittpub>
- From: wil@ittpub.nl (Wil Evers)
- Date: 23 Jan 96 17:38:23 WET
- References: <3102B39F.3CC9@cts.com>
- Distribution: world
- Nntp-Posting-Host: lintilla
-
- In article <3102B39F.3CC9@cts.com> "Steven K. Sharp" <sksharp@cts.com>
- writes:
-
- [snip]
-
- > I feel that they should be using exceptions to handle these things.
- > It's more understood and generally cleaner.
-
- How to do proper error handling using the C++ exception mechanism is still
- a controversial issue. Some experts prefer old tried and tested techniques
- using function return values, error numbers and the like, others feel
- exception handling is superior. There are a few examples around that show
- proper exception generation and -handling when doing complicated stuff can
- be notoriously difficult.
-
- > It also forces handling
- > of the exception rather than being able to skip any error handling.
-
- C++ gives us the option of catching exceptions, it doesn't force us to do
- so. If we forget to catch an exception, the program will abort, which is
- sometimes, but not always, the most reasonable thing to do.
-
- > Which leads to other questions. When should you throw an exception?
- > Should they only be thrown when the program is in such a state that
- > you have no idea what's going on?
-
- When throwing an exception that is supposed to be caught somewhere, we are
- relying on the exception-awareness of various parts of the program. Some
- of these parts may be coming from libraries written before exceptions were
- introduced in the language, or by exception-unware programmers. If this is
- the case, your program will be in an unstable state at the point where you
- catch the exception.
- In general, exception-aware code must be written using RAI (Resource
- Aquisition is Initialization) idioms, which is at odds with more symbolic
- (Smalltalk-like-) programming styles, in which resource management is
- considered to be a non-issue.
-
- All in all, the situation is quite messy, and considering how long it took
- various compiler vendors to properly implement exception handling and the
- amount of legacy code around, it is likely to stay messy for a long time.
- My advice would be:
-
- - Be prepared to properly handle exceptions thrown by others: make sure
- your objects are in a `destructor-ready' state when calling a function
- that might throw one. In principle, all automatic (local) raw pointers to
- heap allocated objects should be replaced by smart pointer objects.
- - Don't rely on the exception-awareness of code you're not familiar with.
- - Don't force users of your code to be exception-aware: only throw an
- exception when you see no other way out. If you do so, clearly state this
- in the documentation of your code.
-
- - Wil
-